package com.ruanmeng.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.JianCeList;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.CircularImage;
import com.example.lenovo.weiyi.R;
import com.example.lenovo.weiyi.ServiceXQActivity;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 定制服务
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class ServiceFragment extends Fragment {

    @BindView(R.id.rlist_service)
    RecyclerView rlistService;
    @BindView(R.id.refresh_service)
    SwipeRefreshLayout mRefresh;
    private View fview;
    private LinearLayoutManager linearLayoutManager;
    private JianCeList model;
    private LinearLayout li_null;
    int page = 1;
    ArrayList<JianCeList.FooterBean> Temp_listfw = new ArrayList<>();//后面追加服务列表
    private JianceListAdapter mAdapter;

    public static ServiceFragment instantiation() {
        ServiceFragment fragment = new ServiceFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        fview = inflater.inflate(R.layout.service_fragment_layout, null);
        ButterKnife.bind(this, fview);
        initView(fview);
        getData();
        return fview;
    }

    private void initView(View vfview) {
        //空
        li_null = (LinearLayout) vfview.findViewById(R.id.li_service_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(getActivity());
        rlistService.setLayoutManager(linearLayoutManager);
        mAdapter = new JianceListAdapter(getActivity(), rlistService, Temp_listfw, R.layout.item_jiance);
        rlistService.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), ServiceXQActivity.class);
                intent.putExtra("aid", Temp_listfw.get(position + 1).getOtoRecordId());
                startActivity(intent);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
//                page++;
//                getData();
            }
        });

    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.JCReportsList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(getActivity(), "uid"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(getActivity(), 0, mRequest,
                new CustomHttpListener(getActivity(), true, JianCeList.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        model = ((JianCeList) data);
                        if (page == 1) {
                            Temp_listfw.clear();
                        }
                        Temp_listfw.addAll(model.getFooter());
                        if (Temp_listfw.size() == 0) {
                            li_null.setVisibility(View.VISIBLE);
                            rlistService.setVisibility(View.GONE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }


    public class JianceListAdapter extends BaseLoadMoreHeaderAdapter<JianCeList.FooterBean> {


        public JianceListAdapter(Context mContext, RecyclerView recyclerView, List<JianCeList.FooterBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, JianCeList.FooterBean s) {
            CircularImage pic = (CircularImage) holder.itemView.findViewById(R.id.imv_jclist_userpic);
            ImageView imv_jclist_sex = (ImageView) holder.itemView.findViewById(R.id.imv_jclist_sex);
            TextView tv_ispg = (TextView) holder.itemView.findViewById(R.id.tv_ispg);
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_jclist_name);
            TextView sg = (TextView) holder.itemView.findViewById(R.id.tv_jclist_sg);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_jslist_time);
            TextView years = (TextView) holder.itemView.findViewById(R.id.tv_jclist_years);
            TextView weight = (TextView) holder.itemView.findViewById(R.id.tv_jclist_tz);
            LinearLayout li_man = (LinearLayout) holder.itemView.findViewById(R.id.li_man);
            //种类-标题
            LinearLayout li_bg_type = (LinearLayout) holder.itemView.findViewById(R.id.li_bg_type);
            TextView tv_bg_type = (TextView) holder.itemView.findViewById(R.id.tv_bg_type);
            //服务
            TextView tv_service_pg = (TextView) holder.itemView.findViewById(R.id.tv_servicelist_pg);
            //检测
            LinearLayout li_jc = (LinearLayout) holder.itemView.findViewById(R.id.li_jc);
            //新加--精确报告+开始锻炼
            TextView tv_jc_report = (TextView) holder.itemView.findViewById(R.id.tv_jc_report);
            TextView tv_jc_start = (TextView) holder.itemView.findViewById(R.id.tv_jc_start);
            LinearLayout li_jiance = (LinearLayout) holder.itemView.findViewById(R.id.li_jiance);
            li_jiance.setVisibility(View.GONE);
            time.setText(s.getCreateDate());
            tv_service_pg.setVisibility(View.VISIBLE);
            li_jc.setVisibility(View.GONE);
            tv_ispg.setVisibility(View.GONE);
            tv_service_pg.setText(s.getPingjia());
            li_bg_type.setVisibility(View.GONE);
            li_bg_type.setOnClickListener(null);
        }
    }

}